#include "../../framework/framework.h"
#include "../inc/xCanvas.h"
#include "../inc/xDevice.h"
#include "../inc/xMedia.h"
namespace Sky
{

	TUint xCanvas::ms_Flag = D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET;
	D3DCOLOR xCanvas::ms_Color = D3DCOLOR_RGBA(0,0,0,0);
	TFloat xCanvas::ms_Zdepth = 1.0f;
	TUint xCanvas::ms_Stencil = 0;
	TResult xCanvas::onCreate( xDxApp* app )
	{
		return XE_OK;
	}

	TResult xCanvas::onCreate( xDevice* device )
	{
		m_Res = device;
		Initialise();
		return XE_OK;
	}
	TResult xCanvas::onDelete( xDxApp* app )
	{
		Finalise();
		return XE_OK;
	}

	TResult xCanvas::onDelete( xDevice* device )
	{
		m_Res = vNull;
		return XE_OK;
	}
	TResult xCanvas::onReset( xDevice* device )
	{
		m_Res = device;
		return XE_OK;
	}

	TResult xCanvas::onLost( xDevice* device )
	{
		m_Res = vNull;
		return XE_OK;
	}

	TResult xCanvas::Attach( xPict* pict,TInt level /*= 0*/ )
	{
		TResult hr;
		xAssertA(pict && (pict->getUsage() & D3DUSAGE_RENDERTARGET) != 0);
		if(pict->getNormalTexture() == vNull)
		{
			myDeV_Ret_M(pict->trySelfCreate());
		}
		if(m_Res == vNull)
		{
			myDeV_Ret_M(trySelfCreate());
		}
		D3DRECT* pRect;
		if(m_bRected)
		{
			pRect = &m_Rect;
		}
		else
			pRect = vNull;
		m_levelPrev = level;
		m_Res->getDevice9()->GetRenderTarget(level,&m_pPrev);
		IDirect3DSurface9* surface;
		myDxV_Ret_hr_M(pict->getNormalTexture()->GetSurfaceLevel(level,&surface),XE_APICALL);
		myDxV_Ret_hr_M(m_Res->getDevice9()->SetRenderTarget(level, surface),XE_APICALL);
		m_curSurf = pict;
		myDxV_Ret_hr_M(m_Res->getDevice9()->Clear(0, pRect, m_Flag,m_Color,m_Zdepth,m_Stencil),XE_APICALL);
		myDxV_Ret_hr_M(m_Res->getDevice9()->BeginScene(),XE_APICALL);

		return XE_OK;
	}

	xCanvas& xCanvas::setRect( D3DRECT* rect )
	{
		if(rect)
		{
			m_Rect = *rect;m_bRected = vTrue;
		}
		else
			m_bRected = vFalse;
		return *this;
	}

	TVoid xCanvas::Detach()
	{
		m_Res->getDevice9()->EndScene();
		m_Res->getDevice9()->SetRenderTarget(m_levelPrev,m_pPrev);
		m_curSurf = vNull;
	}
	TVoid xCanvas::Initialise()
	{
		m_Color = ms_Color;
		m_pPrev = vNull;
		m_bRected = vFalse;
		m_Stencil = ms_Stencil;
		m_Zdepth = ms_Zdepth;
		m_Flag = ms_Flag;
		m_curSurf = vNull;
	}

	TVoid xCanvas::Finalise()
	{

	}

	xCanvas::xCanvas()
	{
		Initialise();
	}

	xCanvas::~xCanvas()
	{
		Finalise();
	}

	xCanvas& xCanvas::setViewPort( D3DVIEWPORT9 * pViewport )
	{
		D3DVIEWPORT9 fullPort;
		if(m_Res == vNull)
			trySelfCreate();
		xAssertA(m_Res); 
		if(pViewport == vNull)
		{
			fullPort.X = 0;
			fullPort.Y = 0;
			fullPort.Width = m_Res->getBackBufferWidth();
			fullPort.Height = m_Res->getBackBufferHeight();
			fullPort.MinZ = 0;
			fullPort.MaxZ = 1.0f;
			pViewport = &fullPort;
		}

		myDxV(m_Res->getDevice9()->SetViewport(pViewport),M);
		return *this;
	}

	xCanvas& xCanvas::getViewPort( D3DVIEWPORT9 * pViewport )
	{
		myDxV(m_Res->getDevice9()->GetViewport(pViewport),M)
			return *this;
	}
}